import { Component, OnInit, Injector } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { NzTableHelper } from '@shared/helpers/NzTableHelper';
import { ServiceProxy, SiteDTO } from '@shared/service-proxy/service-proxies';
import { finalize } from 'rxjs/operators';

import * as moment from 'moment';
import { IfStmt } from '@angular/compiler';
import { HttpHeaders, HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-path-use',
  templateUrl: './path-use.component.html',
  styleUrls: ['./path-use.component.less']
})
export class PathUseComponent extends AppComponentBase implements OnInit {

  tableHelper: NzTableHelper;
  isVisible: boolean = false;

  theTime: moment.Moment[] = [];
  sortList = [];
  siteId: string;
  sites: SiteDTO[] = [];
  sortStr: string = "desc";
  currentSiteId: string;
  constructor(
    injector: Injector,
    private _service: ServiceProxy,
    private httpClient: HttpClient) {
    super(injector);
    this.tableHelper = new NzTableHelper();
  }

  ngOnInit() {
    this.load();

    this.siteId = '';
    this.getSites();
    this.sortList = [
      { label: this.l("desc"), value: 'desc', checked: true },
      { label: this.l("asc"), value: 'asc' }];


    this.currentSiteId = this.appSession.user.siteId;
        
  }
  getSites(): void {
    this._service.getAllSitesUsingGET().subscribe(result => {
      this.sites = result.result.fieldList;
    })
  }

  load(pageIndex?: number) {
    if (typeof pageIndex !== 'undefined') {
      this.tableHelper.pageIndex = pageIndex || 1;
    }
    this.getList();
  }

  sort(sort: { key: string, value: string }): void {
    this.tableHelper.setSorting = sort;
    this.load();
  }

  getList() {
    this._service.usefrequencyUsingGET(this.sortStr, this.tableHelper.pageSize, this.theTime[0], this.theTime[1], undefined, this.siteId).pipe(finalize(() => {
      this.tableHelper.loading = false;
    })).subscribe((result) => {
      this.tableHelper.items = result.result;
    });
  }


  reset() {
    this.theTime = [];
    this.sortStr = 'desc';
    this.siteId = '';
    this.load();
  }
  exportToExcel(): void {
    const nameValues: abp.INameValue[] = [];
    nameValues.push({ name: 'limit', value: this.tableHelper.pageSize });
    nameValues.push({ name: 'sort', value: this.sortStr });
    nameValues.push({ name: 'startTime', value: this.theTime[0] });
    nameValues.push({ name: 'endTime', value: this.theTime[1] });
    nameValues.push({ name: 'destinationsiteId', value: this.siteId });
    const queryString = abp.utils.buildQueryString(nameValues);
    const exportUrl = this.serviceIpUrl + '/api/center/statistics/route/usefrequencyexport' + queryString; // 以上为导出url的拼装，可自行拼装，直接一行拼起来也行

    const options: any = {
      responseType: 'blob',
      headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Accept': 'application/json' })
    };

    this.httpClient.get(exportUrl, options).subscribe(res => {
      const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });// excel专用，其他请自行查阅资料
      const objectUrl = URL.createObjectURL(blob);
      const a = document.createElement('a');
      document.body.appendChild(a);
      a.setAttribute('style', 'display:none');
      a.setAttribute('href', objectUrl);
      const fileName = this.l('path_use_time_statistics') + moment().format('YYYYMMDDHHmmss') + '.xlsx';
      a.setAttribute('download', fileName);
      a.click();
      URL.revokeObjectURL(objectUrl);

    });
  }
}
